package com.oa.srms.apps.business.organization.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.business.organization.ISectionCategoryService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.organization.ISectionCategoryDao;
import com.oa.srms.apps.entity.organization.SectionCategoryValue;

public class SectionCategoryServiceImpl implements ISectionCategoryService {
	@Autowired
	private ISectionCategoryDao categoryDao;
	@Autowired
	private SectionCategoryValue sectionCategory;

	@Override
	public int addSectionCategory(String categoryName) throws Exception {
		sectionCategory.setCategoryName(categoryName);
		sectionCategory.setStates(IDEDStates.ENABLE);
		return categoryDao.addSectionCategory(sectionCategory);
	}

	@Override
	public boolean deleteSectionCategory(String categoryIds) throws Exception {
		String[] categoryIdStr = categoryIds.split(",");
		for(String categoryId : categoryIdStr){
			sectionCategory = queryByCategoryId(Integer.parseInt(categoryId));
			if(sectionCategory != null){
				sectionCategory.setStates(IDEDStates.DELETE);
				categoryDao.editSectionCategory(sectionCategory);
			}else{
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean modifySectionCategory(int categoryId, String categoryName)
			throws Exception {
		sectionCategory = queryByCategoryId(categoryId);
		if(sectionCategory != null){
			sectionCategory.setCategoryName(categoryName);
			return categoryDao.editSectionCategory(sectionCategory);
		}
		return false;
	}

	@Override
	public List<SectionCategoryValue> queryAllCategoryInfo() throws Exception {
		return categoryDao.querySectionCategory(sectionCategory);
	}

	@Override
	public SectionCategoryValue queryByCategoryId(int categoryId)
			throws Exception {
		sectionCategory.setCategoryId(categoryId);
		return categoryDao.queryByCategoryId(sectionCategory);
	}

	@Override
	public List<SectionCategoryValue> querySectionCategory(Integer states)
			throws Exception {
		sectionCategory.setStates(states);
		return categoryDao.querySectionCategory(sectionCategory);
	}

	@Override
	public boolean toDisable(int categoryId) throws Exception {
		sectionCategory = queryByCategoryId(categoryId);
		if(sectionCategory != null){
			sectionCategory.setStates(IDEDStates.DISABLE);
			return categoryDao.editSectionCategory(sectionCategory);
		}
		return false;
	}

	@Override
	public boolean toEnable(int categoryId) throws Exception {
		sectionCategory = queryByCategoryId(categoryId);
		if(sectionCategory != null){
			sectionCategory.setStates(IDEDStates.ENABLE);
			return categoryDao.editSectionCategory(sectionCategory);
		}
		return false;
	}

}
